package com.lineage.server.serverpackets;

import com.lineage.server.model.Instance.L1DeInstance;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.templates.L1PrivateShopBuyList;
import com.lineage.server.templates.L1PrivateShopSellList;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class S_PrivateShop extends ServerBasePacket {
  public S_PrivateShop(L1PcInstance pc, int objectId, int type) {
    L1Object shopObj = World.get().findObject(objectId);
    if (shopObj instanceof L1PcInstance) {
      isPc(pc, objectId, type);
    } else if (shopObj instanceof L1DeInstance) {
      isDe(pc, objectId, type);
    } 
  }
  
  private void isDe(L1PcInstance pc, int objectId, int type) {
    L1DeInstance shopDe = (L1DeInstance)World.get().findObject(objectId);
    if (shopDe == null)
      return; 
    writeC(140);
    writeC(type);
    writeD(objectId);
    if (type == 0) {
      Map<Integer, L1ItemInstance> sellList = new HashMap<>();
      Map<L1ItemInstance, Integer> map = shopDe.get_sellList();
      if (map.isEmpty()) {
        writeH(0);
        return;
      } 
      if (map.size() <= 0) {
        writeH(0);
        return;
      } 
      writeH(map.size());
      int i = 0;
      Iterator<L1ItemInstance> iterator3 = map.keySet().iterator();
      while (iterator3.hasNext()) {
        L1ItemInstance item = iterator3.next();
        writeC(i);
        writeC(item.getBless());
        writeH(item.getItem().getGfxId());
        writeD((int)item.getCount());
        int price = ((Integer)map.get(item)).intValue();
        writeD(price);
        String name = item.getNumberedViewName(item.getCount());
        writeS(name);
        writeC(0);
        sellList.put(Integer.valueOf(i), item);
        i++;
      } 
      (pc.get_otherList()).DELIST.clear();
      (pc.get_otherList()).DELIST.putAll(sellList);
    } else if (type == 1) {
      Map<Integer, int[]> list = shopDe.get_buyList();
      if (list.isEmpty()) {
        writeH(0);
        return;
      } 
      if (list.size() <= 0) {
        writeH(0);
        return;
      } 
      writeH(list.size());
      Iterator<Integer> iterator = list.keySet().iterator();
      while (iterator.hasNext()) {
        int key = ((Integer)iterator.next()).intValue();
        int[] buyitem = list.get(Integer.valueOf(key));
        int count = buyitem[2];
        int level = buyitem[1];
        int price2 = buyitem[0];
        int j = 0;
        Iterator<?> iterator2 = pc.getInventory().getItems().iterator();
        while (iterator2.hasNext()) {
          L1ItemInstance pcItem = (L1ItemInstance)iterator2.next();
          if (pcItem.getItemId() == key && pcItem.getEnchantLevel() == level) {
            writeC(j);
            writeD(pcItem.getId());
            writeD(count);
            writeD(price2);
            j++;
          } 
        } 
      } 
    } 
  }
  
  private void isPc(L1PcInstance pc, int objectId, int type) {
    L1PcInstance shopPc = (L1PcInstance)World.get().findObject(objectId);
    if (shopPc == null)
      return; 
    writeC(140);
    writeC(type);
    writeD(objectId);
    if (type == 0) {
      ArrayList<?> list = shopPc.getSellList();
      if (list.isEmpty()) {
        writeH(0);
        return;
      } 
      int size = list.size();
      if (size <= 0) {
        writeH(0);
        return;
      } 
      pc.setPartnersPrivateShopItemCount(size);
      writeH(size);
      for (int i = 0; i < size; i++) {
        L1PrivateShopSellList pssl = (L1PrivateShopSellList)list.get(i);
        int itemObjectId = pssl.getItemObjectId();
        int count = pssl.getSellTotalCount() - pssl.getSellCount();
        int price = pssl.getSellPrice();
        L1ItemInstance item = shopPc.getInventory().getItem(itemObjectId);
        if (item != null) {
          writeC(i);
          writeC(item.getBless());
          writeH(item.getItem().getGfxId());
          writeD(count);
          writeD(price);
          String name = item.getNumberedViewName(count);
          writeS(name);
          writeC(0);
        } 
      } 
    } else if (type == 1) {
      ArrayList<?> list = shopPc.getBuyList();
      if (list.isEmpty()) {
        writeH(0);
        return;
      } 
      int size = list.size();
      if (size <= 0) {
        writeH(0);
        return;
      } 
      writeH(size);
      for (int i = 0; i < size; i++) {
        L1PrivateShopBuyList psbl = (L1PrivateShopBuyList)list.get(i);
        int itemObjectId = psbl.getItemObjectId();
        int count = psbl.getBuyTotalCount();
        int price = psbl.getBuyPrice();
        L1ItemInstance item = shopPc.getInventory().getItem(itemObjectId);
        for (L1ItemInstance pcItem : pc.getInventory().getItems()) {
          if (item.getItemId() == pcItem.getItemId() && 
            item.getEnchantLevel() == pcItem.getEnchantLevel()) {
            writeC(i);
            writeD(pcItem.getId());
            writeD(count);
            writeD(price);
          } 
        } 
      } 
    } 
  }
  
  public byte[] getContent() {
    return getBytes();
  }
}
